package test;

import repast.simphony.parameter.Parameters;

import defectsim.SimulationEnvironment;
import defectsim.agents.DefectAgent;
import defectsim.agents.DefectFactory;
import defectsim.agents.Dislocation;
import defectsim.agents.EdgeDislocation;
import defectsim.agents.PointDefect;
import defectsim.agents.DefectFactory.DefectType;
import defectsim.material.SlipSystem;

public class DislocationSourceTester3 extends DefectAgent{

	private Dislocation tester = null;

	private SimulationEnvironment env = SimulationEnvironment.getInstance();
	private double gridScale;

	@Override
	public void percept() {
	}

	@Override
	public void mentalUpdate() {
	}

	@Override
	public void act() {
		if (env.getCurrentTick() == 1.0)
			this.multiply(1000);
	}

	public DislocationSourceTester3(){
		Parameters p = env.getParameters();
		int gridSize = (Integer)p.getValue("gridsize");		
		double boxLength = (Double)p.getValue("spacelength");
		this.gridScale = gridSize / boxLength;
	}

	private void multiply(int noOfDislocations){
		int s = 0;
		SlipSystem[] slipsys = env.getMaterial().getSlipSystems();
		EdgeDislocation dislocation = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation.setSlipSystem(slipsys[s]);
		dislocation.setUnitBurgersVector(1);
		dislocation.setX(4.8);
		dislocation.setY(5);
		dislocation.setLocationGridX((int)(5 * gridScale));
		dislocation.setLocationGridY((int)(5 * gridScale));
		env.nucleate(dislocation);
		PointDefect pd = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
		pd.setX(5);
		pd.setY(5);
		env.nucleate(pd);
		EdgeDislocation dislocation1 = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation1.setSlipSystem(slipsys[s]);
		dislocation1.setUnitBurgersVector(-1);
		dislocation1.setX(5.2);
		dislocation1.setY(5.1);
		dislocation1.setLocationGridX((int)(5 * gridScale));
		dislocation1.setLocationGridY((int)(5.1 * gridScale));
		env.nucleate(dislocation1);
		PointDefect pd1 = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
		pd1.setX(5);
		pd1.setY(5.1);
		env.nucleate(pd1);
		EdgeDislocation dislocation2 = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation2.setSlipSystem(slipsys[s]);
		dislocation2.setUnitBurgersVector(1);
		dislocation2.setX(4.8);
		dislocation2.setY(5.2);
		dislocation2.setLocationGridX((int)(5 * gridScale));
		dislocation2.setLocationGridY((int)(5.2 * gridScale));
		env.nucleate(dislocation2);
		PointDefect pd2 = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
		pd2.setX(5);
		pd2.setY(5.2);
		env.nucleate(pd2);
		EdgeDislocation dislocation3 = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation3.setSlipSystem(slipsys[s]);
		dislocation3.setUnitBurgersVector(-1);
		dislocation3.setX(5.2);
		dislocation3.setY(5.3);
		dislocation3.setLocationGridX((int)(5 * gridScale));
		dislocation3.setLocationGridY((int)(5.3 * gridScale));
		env.nucleate(dislocation3);
		PointDefect pd3 = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
		pd3.setX(5);
		pd3.setY(5.3);
		env.nucleate(pd3);
		EdgeDislocation dislocation4 = (EdgeDislocation) DefectFactory.createDefect(DefectType.EdgeDislocation);
		dislocation4.setSlipSystem(slipsys[s]);
		dislocation4.setUnitBurgersVector(1);
		dislocation4.setX(4.8);
		dislocation4.setY(5.4);
		dislocation4.setLocationGridX((int)(5 * gridScale));
		dislocation4.setLocationGridY((int)(5.4 * gridScale));
		env.nucleate(dislocation4);
		PointDefect pd4 = (PointDefect) DefectFactory.createDefect(DefectType.PointDefect);
		pd4.setX(5);
		pd4.setY(5.4);
		env.nucleate(pd4);

	}

	public Dislocation getDislocationTester(){
		return tester;
	}

}
